package com.cloudbroker.bcs.platform.ltb.middle.bizservice.impl;

import com.alibaba.fastjson.JSON;
import com.cloudbroker.bcs.common.annotation.NoLogin;
import com.cloudbroker.bcs.common.annotation.NoToken;
import com.cloudbroker.bcs.common.entity.HSBCPSBackVO;
import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.platform.ltb.bizapi.constants.HSBCPSErrorCodes;
import com.cloudbroker.bcs.platform.ltb.bizapi.constants.status.CloseStatus;
import com.cloudbroker.bcs.platform.ltb.bizapi.constants.status.ReplyStatus;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.AdminFeedbackBizService;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.feedback.FeedbackBizService;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.feedback.back.FeedbackAdminList;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.feedback.back.FeedbackAdminVo;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.feedback.back.FeedbackSubBack;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.feedback.vo.FeedBackReply;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.feedback.vo.FeedbackAdminForm;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.feedback.vo.PublicManageFeedbackQryForm;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.feedback.vo.PublicManageFeedbackReplyForm;
import com.cloudbroker.bcs.platform.ltb.middle.entity.Feedback;
import com.cloudbroker.bcs.platform.ltb.middle.entity.UserEntity;
import com.cloudbroker.bcs.platform.ltb.middle.service.FeedBackService;
import com.cloudbroker.bcs.platform.ltb.middle.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by fangyf18371 on 2016/7/22.
 * 管理台意见反馈回复、查询
 */
@Service
public class AdminFeedbackService implements AdminFeedbackBizService {
    @Resource
    private FeedBackService feedBackService;

    @Resource
    private UserService userService;

    /**
     * 反馈意见回复
     * @param feedBackReply
     * @return
     */
    @Override
    public FeedbackSubBack feedback_reply(FeedBackReply feedBackReply) {
        Feedback feedback=feedBackService.get(feedBackReply.getFeedback_id());
        if (feedback == null) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.FeedbackErrorInfo.FEEDBACK_NOT_EXIST,
                    FeedbackSubBack.class);
        }
        if (feedback.getReply_time()!=null&&feedback.getReply_time()>0){
            return VOUtil.genErrorResult(HSBCPSErrorCodes.FeedbackErrorInfo.FEEDBACK_ALREADY_REPLY,
                    FeedbackSubBack.class);
        }
        feedback.setReply_content(feedBackReply.getReply_content());
        feedback.setReply_time(System.currentTimeMillis());
        feedback.setUpdate_time(System.currentTimeMillis());
        feedBackService.update(feedback);
        FeedbackSubBack feedbackSubBack = new FeedbackSubBack();
        return  VOUtil.setSuccessResult(feedbackSubBack);
    }

    /**
     * 反馈意见查询
     * @param feedbackAdminForm
     * @return
     */
    @Override
    public FeedbackAdminList feedback_admin_query(FeedbackAdminForm feedbackAdminForm) {
        List<Feedback> feedbacks=feedBackService.getFeedBack(feedbackAdminForm);
        List<FeedbackAdminVo> feedbackAdminList= JSON.parseArray(JSON.toJSONString(feedbacks),FeedbackAdminVo.class);
        for (FeedbackAdminVo feedbackAdminVo : feedbackAdminList) {
            if(feedbackAdminVo.getReply_content()==null || feedbackAdminVo.getReply_content()=="") {
                feedbackAdminVo.setIs_reply(String.valueOf(ReplyStatus.NO_REPLY.getVal()));
            }else {
                feedbackAdminVo.setIs_reply(String.valueOf(ReplyStatus.ALREADY_REPLY.getVal()));
            }
            UserEntity user = userService.get(feedbackAdminVo.getFeedbacker());
            if (user!=null) {
                feedbackAdminVo.setAlias_name(user.getAlias_name());
            }else {
                feedbackAdminVo.setAlias_name("");
            }
        }
        String data_str = JSON.toJSONString(feedbackAdminList);
        FeedbackAdminList feedbackList=new FeedbackAdminList();
        feedbackList.setData_str(data_str);
        feedbackList.setTotal_item_num(feedBackService.countFeedBack(feedbackAdminForm));
        return  VOUtil.setSuccessResult(feedbackList);
    }


    @NoToken
    @NoLogin
    public FeedbackSubBack public_manage_feedback_reply(PublicManageFeedbackReplyForm publicManageFeedbackReplyForm){
        FeedBackReply feedBackReply = new FeedBackReply();
        feedBackReply.setCompany_id(publicManageFeedbackReplyForm.getCompany_id());
        feedBackReply.setFeedback_id(publicManageFeedbackReplyForm.getFeedback_id());
        feedBackReply.setReply_content(publicManageFeedbackReplyForm.getReply_content());
        return feedback_reply(feedBackReply);
    }

    @NoToken
    @NoLogin
    public FeedbackAdminList public_manage_feedback_admin_query(PublicManageFeedbackQryForm form){
        FeedbackAdminForm feedbackAdminForm = new FeedbackAdminForm();
        feedbackAdminForm.setCompany_id(form.getCompany_id());
        feedbackAdminForm.setIs_reply(form.getIs_reply());
        feedbackAdminForm.setPage_no(form.getPage_no());
        feedbackAdminForm.setRequest_num(form.getRequest_num());
        return feedback_admin_query(feedbackAdminForm);
    }
//    /**
//     * 反馈回复
//     * @param feedBackReply
//     * @return
//     */
//    public FeedbackSubBack feedback_reply(FeedBackReply feedBackReply){
////        Date now = new Date();
////        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
////        Long replyTime=Long.parseLong(dateFormat.format( now ));
////        feedBackReply.setReply_time(replyTime);
////        FeedbackSubBack feedbackSubBack=new FeedbackSubBack();
//        return null;
//    }
//
//    public FeedbackAdminList feedback_admin_query(FeedbackAdminForm feedbackAdminForm){
////        FeedbackList list=new FeedbackList();
////        List<FeedbackResult> feedbackResultList=new ArrayList<>();
////        Feedback feedback=new Feedback();
////        for(FeedbackResult feedbackResult:feedbackResultList){
////            FeedbackResult feedResult=new FeedbackResult();
////            feedResult.setFeedback_id(feedback.getFeedback_id());
////            feedResult.setContent(feedback.getContent());
////            feedResult.setReply_time(feedback.getReply_time());
////            feedResult.setUpdate_time(feedback.getUpdate_time());
////            feedResult.setCreate_time(feedback.getCreate_time());
////
////            feedbackResultList.add(feedbackResult);
////        }
//////        feedBackService.queryAll();
////        // feedBackService.queryWithPagination(feedback,initpage)
////        list.setData_list(feedbackResultList);
//        return  null;
//    }
}
